import * as actions from '../actionTypes/'

const project = (
    state = {
        showPublishModal: false,
        showProjectModal: false,
        showQRCode: false,
        projectNameInput: {
            show: false,
            type: 'save', //save save-as
        },
        isLoading: false,
        isLoadingProject: false,
        showTurtleOrResult: true, //true是turtle
        saveStatus: 'save', //save saving success error
        projectName: '',
        projectDesc: '',
        projectId: null,
        all: { list: [] },
        unpublish: { list: [] },
        publish: { list: [] },
    },
    action
) => {
    switch (action.type) {
        case actions.TOGGLE_PUBLISH_MODAL:
            return {
                ...state,
                showPublishModal: action.payload,
            }
        case actions.CHANGE_PROJECT_NAME:
            return {
                ...state,
                projectName: action.payload,
            }
        case actions.SET_PROJECT_ID:
            return {
                ...state,
                projectId: action.payload,
            }
        case actions.CHANGE_PROJECT_DESC:
            return {
                ...state,
                projectDesc: action.payload,
            }
        case actions.TOGGLE_PUBLISH_LOADING:
            return {
                ...state,
                isLoading: action.payload,
            }
        case actions.LOAD_PROJECT_SUCCESS:
            const { isSearch } = action.payload
            const newList = action.payload.data.list
            const oldList = isSearch ? [] : state[action.payload.listType].list
            return {
                ...state,
                [action.payload.listType]: {
                    ...action.payload.data,
                    list: oldList.concat(newList),
                },
            }
        case actions.TOGGLE_PROJECT_MODAL:
            return {
                ...state,
                showProjectModal: action.payload,
            }
        case actions.TOGGLE_LOADING_PROJECT:
            return {
                ...state,
                isLoadingProject: action.payload,
            }
        case actions.SET_PROJECT_SAVE_STATUS:
            return {
                ...state,
                saveStatus: action.payload,
            }
        case actions.CREATE_NEW_PROJECT:
            return {
                ...state,
                projectName: '',
                projectDesc: '',
                projectId: null,
            }
        case actions.SET_PROJECT_ITEM:
            const d = action.payload
            return {
                ...state,
                projectName: d.projectName,
                projectDesc: d.comment,
                projectId: d.id,
                lessonId: d.lessonId,
            }
        // case actions.SAVE_PROJECT_AS:
        //     return {
        //         ...state,
        //         showSaveAsProjectName: true,
        //     }
        case actions.TOGGLE_PROJECT_NAEM_MODAL:
            return {
                ...state,
                projectNameInput: action.payload,
            }
        case actions.TOGGLE_QRCODE:
            return {
                ...state,
                showQRCode: action.payload,
            }
        case actions.SHOW_TURTLE_OR_RESULT:
            return {
                ...state,
                showTurtleOrResult: action.payload,
            }
        default:
            return state
    }
}

export default project



// WEBPACK FOOTER //
// ./src/reducers/project.js